package com.jrtc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Diseasecategory;
import com.jrtc.base.entity.vo.DiseasecategoryVo;
import com.jrtc.dao.DiseasecategoryDao;
import com.jrtc.service.DiseasecategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 疾病分类表(Diseasecategory)表服务实现类
 *
 * @author makejava
 * @since 2022-03-21 17:34:12
 */
@Service("diseasecategoryService")
public class DiseasecategoryServiceImpl implements DiseasecategoryService
{
    @Resource
    private DiseasecategoryDao diseasecategoryDao;

    /**
     * 通过ID查询单条数据
     *
     * @param diseasecategoryId 主键
     * @return 实例对象
     */
    @Override
    public Diseasecategory queryById(Integer diseasecategoryId) {
        return this.diseasecategoryDao.queryById(diseasecategoryId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Diseasecategory> queryAllByLimit(int offset, int limit) {
        return this.diseasecategoryDao.queryAllByLimit(offset, limit);
    }

    @Override
    public IPage<Diseasecategory> queryDiseasecategoryList(Diseasecategory diseasecategory)
    {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Diseasecategory> page = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(diseasecategory.getPageNo(), diseasecategory.getPageSize());
        IPage<Diseasecategory> diseasecategoryPage = this.diseasecategoryDao.queryDiseasecategoryList(page, diseasecategory);
        return diseasecategoryPage;
    }
    /**
     * 新增数据
     *
     * @param diseasecategory 实例对象
     * @return 实例对象
     */
    @Override
    public Diseasecategory insert(Diseasecategory diseasecategory) {
        this.diseasecategoryDao.insert(diseasecategory);
        return diseasecategory;
    }

    /**
     * 修改数据
     *
     * @param diseasecategory 实例对象
     * @return 实例对象
     */
    @Override
    public Diseasecategory update(Diseasecategory diseasecategory) {
        this.diseasecategoryDao.update(diseasecategory);
        return this.queryById(diseasecategory.getDiseasecategoryId());
    }

    /**
     * 通过主键删除数据
     *
     * @param diseasecategoryId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer diseasecategoryId) {
        return this.diseasecategoryDao.deleteById(diseasecategoryId) > 0;
    }

    /**
     * 查询疾病分类信息
     * */
    @Override
    public List<DiseasecategoryVo> seleteDiseasecategory(Integer diseasecategoryId) throws Exception {
        List<Diseasecategory> diseasecategories;
        if (diseasecategoryId != null && diseasecategoryId != 0){
            diseasecategories = diseasecategoryDao.seleteDiseasecategoryByDiseasecategoryId(diseasecategoryId);
        }
        else {
            diseasecategories = diseasecategoryDao.seleteDiseasecategory();
        }
        if (diseasecategories != null && diseasecategories.size() > 0){
            List<DiseasecategoryVo> diseasecategoryVoList = new ArrayList<>();
            for (Diseasecategory diseasecategory : diseasecategories) {
                DiseasecategoryVo diseasecategoryVo = new DiseasecategoryVo();
                diseasecategoryVo.setDiseasecategoryId(diseasecategory.getDiseasecategoryId());
                diseasecategoryVo.setDiseasecategoryName(diseasecategory.getDiseasecategoryName());
                diseasecategoryVoList.add(diseasecategoryVo);
            }
            return diseasecategoryVoList;
        }
        else {
            throw new Exception("未查到疾病分类信息");
        }
    }

    /**
     * 疾病分类信息列表
     * */
    @Override
    public List<Diseasecategory> diseasecategoryList() {
        List<Diseasecategory> diseasecategories = diseasecategoryDao.seleteDiseasecategory();
        return diseasecategories;
    }
}